package br.com.lambdacore.lang;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;

import br.com.lambdacore.lang.annotation.EqualsHashCode;
import br.com.lambdacore.lang.reflect.AnnotationUtils;

public class HashCodeBuilder extends
		org.apache.commons.lang.builder.HashCodeBuilder {

	@SuppressWarnings("unchecked")
	public HashCodeBuilder(Object thiz) {
		try {
			Collection<Field> fields = AnnotationUtils.getAllFieldsAnnoted(thiz
					.getClass(), EqualsHashCode.class);
			for (Field field : fields) {
				field.setAccessible(true);
				this.append(field.get(thiz));
			}
			Collection<Method> methods = AnnotationUtils.getAllMethodsAnnoted(
					thiz.getClass(), EqualsHashCode.class);
			for (Method method : methods) {
				method.setAccessible(true);
				this.append(method.invoke(thiz));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public int hashCode() {
		return this.toHashCode();
	}

	public static HashCodeBuilder newInstance(Object thiz) {
		return new HashCodeBuilder(thiz);
	}

}
